package com.studerb.nntp;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.nntp.NNTPReply;
import org.apache.log4j.Logger;

public class GroupCommandReply {
    private final static Logger logger = Logger.getLogger(GroupCommandReply.class);
    private int replyCode;
    private int postsCount;
    private int firstPost;
    private int lastPost;
    private String groupName;

    public GroupCommandReply(int replyCode, int articleCount, int firstPost, int lastPost, String groupName) {
        this.replyCode = replyCode;
        this.postsCount = articleCount;
        this.firstPost = firstPost;
        this.lastPost = lastPost;
        this.groupName = groupName;
    }

    public int getReplyCode() {
        return replyCode;
    }

    public void setReplyCode(int replyCode) {
        this.replyCode = replyCode;
    }

    public int getArticleCount() {
        return postsCount;
    }

    public void setArticleCount(int articleCount) {
        this.postsCount = articleCount;
    }

    public int getFirstPost() {
        return firstPost;
    }

    public void setFirstPost(int firstPost) {
        this.firstPost = firstPost;
    }

    public int getLastPost() {
        return lastPost;
    }

    public void setLastPost(int lastPost) {
        this.lastPost = lastPost;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    /**
     * @param line Line of text as reply from Group Command The form is: 'a b c d e' where a is integer successful reply
     * code which should be 211, b is posts count, c is first post number, d is last post number, and e is the name of
     * the group as a string
     * @return a {@link GroupCommandReply GroupcommandReply} object or null if there is a parsing error
     */
    public static GroupCommandReply parseGroupCommandReply(String line) {
        String[] splitLine = StringUtils.split(line);
        int replyCode, articleCount, firstPost, lastPost;

        if(splitLine.length != 5){
            logger.warn("Error parsing Group Command Reply: " + line);
            return null;
        }
        try{
            replyCode = Integer.parseInt(splitLine[0]);
            articleCount = Integer.parseInt(splitLine[1]);
            firstPost = Integer.parseInt(splitLine[2]);
            lastPost = Integer.parseInt(splitLine[3]);
        }
        catch(NumberFormatException e){
            logger.warn("Error parsing Group Command Reply: " + line);
            return null;
        }
        if(replyCode != NNTPReply.GROUP_SELECTED){
            logger.warn("Bad reply Code from Group Command: " + replyCode);
        }

        return new GroupCommandReply(replyCode, articleCount, firstPost, lastPost, splitLine[4]);
    }

    @Override public String toString() {
        return String.format("Got Group Command Reply %d\t%d\t%d\t%d\t%s", this.getReplyCode(), this.getArticleCount(), this.getFirstPost(), this
                        .getLastPost(), this.getGroupName());
    }
}
